window.onload = function () {
    getArticle();
    innerInfo();
}

// 博客文章内容渲染
function getArticle() {
    console.log('getArticle');
    // 从url中获取id
    var id = window.location.search.split('=')[1];
    console.log(id);
    
    $.ajax({
        type: 'get',
        url: '/api/articles/' + id,
        success: function (data) {
            console.log(data);
            var article_info = $('.continer');
            article_info.append(`
            <div class="part_title">${data.title}</div>
        <div class="part_decoration"><img src="./img/xhx_01.jpg" alt=""></div>
            <div class="articleAuthor">${data.author}</div>
            <div class="careateTime">创建时间：${data.createdAt}</div>
            <div class="articleBody">${data.content}</div>
            `)
        },
        error: function (err) {
            console.log(err);
        }
    })
}
// 填写用户名
function innerInfo() {
    var banner = $('.top_banner_user')
    var banner_info = $('.top_banner_info')
    if (localStorage.getItem("token")) {
        // jquery ajax请求 验证token
        $.ajax({
            type: 'post',
            url: '/api/user/verify',
            beforeSend: function (request) {
                request.setRequestHeader("Authorization", localStorage.getItem("token"));
            },
            success: function (data) {
                banner.css('display', 'none')
                banner_info.css('display', 'block')
                banner_info.append(`
                <a href="#">用户: ${data.username}</a>`
                );
                console.log(data.username)
            },
            error: (err) => {
                console.log(err)
            }
        })
    }
}